﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZLFMSTInputDialog
{
    public partial class InputBox_PLCSN : Form
    {
        #region 内部功能
        private List<byte> fPLCSNBytes = new List<byte>();
        public byte[] PLCSNBytes { get { return fPLCSNBytes.ToArray(); } }

        public InputBox_PLCSN(string fDefaultStr, string fTitle)
        {
            InitializeComponent();
            textBoxPLCSN.Text = fDefaultStr;
            Text = fTitle;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((fCheckPLCSNHex(textBoxPLCSN.Text)) &&
                (textBoxPLCSN.Text.Length==24))
            {
                string fStrTemp = "";
                for(int i=0;i<24;i++)
                {
                    fStrTemp += textBoxPLCSN.Text[i];
                    if ((i % 2 == 1)&&(i!=23))
                        fStrTemp += ":";
                }
                string[] fStrList = fStrTemp.Split(':');
                fPLCSNBytes.Clear();
                foreach (string fS in fStrList)
                    fPLCSNBytes.Add(Convert.ToByte(fS, 16));
                this.DialogResult = DialogResult.OK;
                Close();
            }
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (fCheckPLCSNHex(textBoxPLCSN.Text))
                textBoxSNLen.Text = textBoxPLCSN.Text.Length.ToString();
            else
                textBoxSNLen.Text = "非法字符";
        }

        private bool fCheckPLCSNHex(string fPLCSNStr)
        {
            const string PATTERN = @"[A-Fa-f0-9]+$";
            return System.Text.RegularExpressions.Regex.IsMatch(fPLCSNStr, PATTERN);
        }

        private void buttonRandom_Click(object sender, EventArgs e)
        {
            Random fRd = new Random();
            textBoxPLCSN.Text = "";
            for (int i = 0; i < 12; i++)
                textBoxPLCSN.Text += fRd.Next(0, 0x100).ToString("X2");
        }

        #endregion

        #region 外部全局功能

        public static bool InputPLCSN(ref string fValue, string fTitle, out byte[] fPLCSNBytes)
        {
            fPLCSNBytes = null;
            if ((!string.IsNullOrEmpty(fValue)) && (fValue.Length != 24))
                return false;
            else
            {
                InputBox_PLCSN fInputForm = new InputBox_PLCSN(fValue,  fTitle);
                if (fInputForm.ShowDialog() == DialogResult.OK)
                {
                    fValue = fInputForm.textBoxPLCSN.Text;
                    fPLCSNBytes = fInputForm.PLCSNBytes;
                    return true;
                }
            }
            return false;
        }

        public static bool InputPLCSN(ref string fValue,out byte[] fPLCSNBytes)
        {            
            return InputPLCSN(ref  fValue,  "输入PLC序列号",out fPLCSNBytes);
        }
        #endregion
    }
}
